package com.zscat.mallplus.controller;


import com.jpay.weixin.api.WxPayApi;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.dms.service.IQsgRechargeOrderService;
import com.zscat.mallplus.dms.service.IQsgStoreService;
import com.zscat.mallplus.qsg.QsgRechargeOrder;
import com.zscat.mallplus.qsg.QsgStore;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.WechatUtil;
import com.zscat.mallplus.utils.XmlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 分账api接口
 */
@RestController
@Api(tags = "SharingOrderApi", description = "分账Api相关接口")
@RequestMapping("/api/sharing")
public class SharingOrderApi {


    @Autowired
    private IQsgStoreService qsgStoreService;

    @Autowired
    private IQsgRechargeOrderService rechargeOrderService;

    @Autowired
    private IDmsOrderService orderService;

    @IgnoreAuth
    @ApiOperation("根据条件查询所有充值套餐套餐管理列表")
    @GetMapping(value = "/completeSharingByIdAndType")
    public Object completeSharingByOrder(Long orderId,String type) {
        Map<String, Object> resultUn =  new HashMap<>();
        try {
            if("order".equalsIgnoreCase(type)){
                DmsOrder orderInfo = orderService.getById(orderId);
                QsgStore store = qsgStoreService.getById(orderInfo.getStoreId());
                Map<String, String> parame = new TreeMap<>();
                parame.put("mch_id", store.getServerMchId());//服务商
                parame.put("appid", store.getServerAppid());//服务商appid
                parame.put("sub_mch_id", store.getMchId());//出资方 根据订单的storeId获取
                parame.put("transaction_id", orderInfo.getWxOrderSn());//微信订单号 根据订单获取
                parame.put("nonce_str", System.currentTimeMillis() + "");
                parame.put("out_order_no",orderInfo.getOrderSn());
                parame.put("description","分账已完成");
                String sign = WechatUtil.arrayHmacsha(parame,store.getServerKey());
                parame.put("sign",sign);
                //上线后需改为线上模式
                String i = WxPayApi.profitsharingfinish(parame, store.getServerCertPath(), store.getServerMchId());//参数 证书地址 服务商证书密码默认即商户号
                resultUn = XmlUtil.xmlStrToMap(i);
            }else if("recharge".equalsIgnoreCase(type)){
                QsgRechargeOrder orderInfo = rechargeOrderService.getById(orderId);
                QsgStore store = qsgStoreService.getById(orderInfo.getStoreId());
                Map<String, String> parame = new TreeMap<>();
                parame.put("mch_id", store.getServerMchId());//服务商
                parame.put("appid", store.getServerAppid());//服务商appid
                parame.put("sub_mch_id", store.getMchId());//出资方 根据订单的storeId获取
                parame.put("transaction_id", orderInfo.getWxOrderSn());//微信订单号 根据订单获取
                parame.put("nonce_str", System.currentTimeMillis() + "");
                parame.put("out_order_no",orderInfo.getOrderSn());
                parame.put("description","分账已完成");
                String sign = WechatUtil.arrayHmacsha(parame,store.getServerKey());
                parame.put("sign",sign);
                //上线后需改为线上模式
                String i = WxPayApi.profitsharingfinish(parame, store.getServerCertPath(), store.getServerMchId());//参数 证书地址 服务商证书密码默认即商户号
                resultUn = XmlUtil.xmlStrToMap(i);
            }
            return new CommonResult().success(resultUn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }
}
